const OSS = require('ali-oss');
const fs = require('fs');
// 初始化OSS客户端
const client = new OSS({
    // 从环境变量中获取AccessKey ID的值
    accessKeyId: "LTAI5tGzqeVah18znseETe79",
    // 从环境变量中获取AccessKey Secret的值
    accessKeySecret: "Syz5WwktW97mZhBuxNEzf7vlrIRjCE",
    // 指定OSS的Endpoint
    endpoint: 'oss-cn-chengdu.aliyuncs.com',
    // 指定Bucket名称
    bucket: 'my-website1'
});

async function putOSS (src,dist) {
    try {
        let result = await client.put(dist, src);
    } catch (e) {
        console.log(e);
    } finally {
        console.log("已成功部署至OSS")
    }
}
function addFileToOSSSync(src,dist){
    var docs = fs.readdirSync(src);
    docs.forEach(function(doc){
        var _src = src + '/' + doc,
            _dist = dist + '/' + doc;
        var st = fs.statSync( _src);
        // 判断是否为文件
        if( st.isFile()&&doc!=='.DS_Store' ){
            putOSS(_src,_dist);
        }
        // 如果是目录则递归调用自身
        else if( st.isDirectory() ){
            addFileToOSSSync( _src,_dist);
        }
    })
}
addFileToOSSSync( "build",'')
